package com.batchbrake.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;

import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXTreeTable;

import com.batchbrake.buttons.ActionButton;
import com.batchbrake.queue.QueueBuilder;
import com.batchbrake.queue.QueuePanel;
import com.batchbrake.queue.actions.AddFileAction;
import com.batchbrake.queue.actions.AddFolderAction;
import com.batchbrake.queue.actions.EditPresetAction;
import com.batchbrake.queue.actions.RemoveSelectedAction;
import com.batchbrake.queue.actions.SetOutputFolderAction;

public class BatchBrakePanel extends JXPanel implements ActionListener {
	private static final ImageIcon icnAddFolder = new ImageIcon("images/AddFolder.png"); 
	private QueuePanel queuePanel = null;
	private QueueBuilder builder = null;
	public JComboBox cboPresets = null;
	private BatchBrakeFrame frame = null;
	
	public BatchBrakePanel(BatchBrakeFrame frame, BatchBrakeProc proc, QueueBuilder builder) {
		this.builder = builder;
		this.frame = frame;
		this.initComponents(proc);
	}
	
	private void initComponents(BatchBrakeProc proc) {
		JXPanel actionPanel = new JXPanel(new BorderLayout());
		Box actionBox = new Box(BoxLayout.Y_AXIS);
		queuePanel = new QueuePanel(proc);
		
//		 AbstractPainter fgPainter = (AbstractPainter)btnAddFile.getForegroundPainter();
//		  StackBlurFilter filter = new StackBlurFilter();
//		  fgPainter.setFilters(filter);
//		  btnAddFile.setForegroundPainter(fgPainter);
		
		JButton btnAddFile = new ActionButton(new AddFileAction(builder));
		btnAddFile.setIcon(new ImageIcon("images/AddMovieIcon.png"));
		btnAddFile.setPreferredSize(new Dimension(200, 20));
		btnAddFile.setMinimumSize(new Dimension(200, 20));
		JButton btnAddFolder = new ActionButton(new AddFolderAction(builder));
		btnAddFolder.setIcon(icnAddFolder);
		btnAddFolder.setPreferredSize(new Dimension(200, 20));
		btnAddFolder.setMinimumSize(new Dimension(200, 20));
		JButton btnRemoveSelected = new ActionButton(new RemoveSelectedAction(queuePanel, proc));
		btnRemoveSelected.setIcon(new ImageIcon("images/RemoveMovieIcon.png"));
		btnRemoveSelected.setPreferredSize(new Dimension(200, 20));
		btnRemoveSelected.setMinimumSize(new Dimension(200, 20));

		JButton btnSetOutputFolder= new ActionButton(new SetOutputFolderAction(frame));
		btnSetOutputFolder.setIcon(new ImageIcon("images/OutputFolder.png"));
		btnSetOutputFolder.setPreferredSize(new Dimension(200, 20));
		btnSetOutputFolder.setMinimumSize(new Dimension(200, 20));

		try { /* Building contents of cboPresets with the result of HandbrakeCLI.getPresets */
			Map<String, List<String>> presets = HandbrakeCLI.getPresets();
			Iterator<String> it = presets.keySet().iterator();
			List<String> shorts = new ArrayList<String>();
			while( it.hasNext() ) {
				String key = it.next();
				for( int i = 0; i < presets.get(key).size(); i++ ) {
					shorts.add(presets.get(key).get(i));
				}
			}

			cboPresets = new JComboBox(shorts.toArray());
			cboPresets.setPreferredSize(new Dimension(200,20));
			cboPresets.setMaximumSize(new Dimension(200,20));
			cboPresets.setMinimumSize(new Dimension(200,20));
			cboPresets.setVisible(true);
			cboPresets.addActionListener(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		actionPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 20));
		actionPanel.setBackground(Color.decode("#d4d8e1"));
		
		actionBox.add(btnAddFile);
		actionBox.add(btnAddFolder);
		actionBox.add(btnRemoveSelected);
		actionBox.add(btnSetOutputFolder);
		
		actionPanel.add(BorderLayout.NORTH, actionBox);
		actionPanel.add(BorderLayout.SOUTH, cboPresets);

		this.setLayout(new BorderLayout());
		this.add(actionPanel, BorderLayout.WEST);
		this.add(queuePanel, BorderLayout.CENTER);
	}
	
	public void setTimerEnabled(boolean enabled) {
		queuePanel.setTimerEnabled(enabled);
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		if( ae.getSource().equals(cboPresets) ) {
			frame.lblPreset.setText("<html><b>Preset : </b>" + cboPresets.getSelectedItem() + "</html>");
		}
	}
}
